package com.kylin.common.core.config;

import com.baomidou.mybatisplus.annotation.DbType;
import com.baomidou.mybatisplus.extension.plugins.MybatisPlusInterceptor;
import com.baomidou.mybatisplus.extension.plugins.inner.InnerInterceptor;
import com.baomidou.mybatisplus.extension.plugins.inner.OptimisticLockerInnerInterceptor;
import com.baomidou.mybatisplus.extension.plugins.inner.PaginationInnerInterceptor;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.autoconfigure.condition.ConditionalOnProperty;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

import java.util.List;

/**
 * mybatis-plus配置类
 *
 * @author wuhao
 * @version 1.0 - 2019/6/24
 */
@ConditionalOnProperty(prefix = "la.datascope", name = "enable", havingValue = "false", matchIfMissing = true)
@Configuration
public class MybatisPlusConfig {

    /** 其他拓展拦截器 */
    @Autowired(required = false)
    private List<InnerInterceptor> additionalInterceptors;

    /**
     * 分页插件
     */
    @Bean
    public MybatisPlusInterceptor mybatisPlusInterceptor() {
        MybatisPlusInterceptor interceptor = new MybatisPlusInterceptor();
        // 乐观锁插件
        interceptor.addInnerInterceptor(new OptimisticLockerInnerInterceptor());

        if (additionalInterceptors != null) {
            additionalInterceptors.forEach(interceptor::addInnerInterceptor);
        }

        // 分页插件，需要再后面，否则select count未被其他拦截器拦截
        interceptor.addInnerInterceptor(new PaginationInnerInterceptor(DbType.MYSQL));

        return interceptor;
    }

}
